/*
 * Gcode.h
 *
 *  Created on: Mar 13, 2011
 *      Author: Jason
 */
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "bed.h"

#ifndef GCODE_H_
#define GCODE_H_

#define _G	0x0001
#define _M	0x0002
#define _T	0x0004
#define _S	0x0008
#define _P	0x0010
#define _X	0x0020
#define _Y	0x0040
#define _Z	0x0080
#define _F	0x0100
#define _E	0x0200
#define _N	0x0300

enum gfunc {G,M,T,S,P,X,Y,Z,I,J,F,R,Q,E,N};

class gRecord
{
public:
	gRecord();
	void init(void);
	void del(void);
	void parse(char *);
	double get(gfunc);
	int getb(gfunc);
	void next (gRecord*);
	gRecord* next(void);

private:
	gRecord *_next;
	double 	_g,
			_m,
			_t,
			_s,
			_p,
			_x,
			_y,
			_z,
			_f,
			_e,
			_n;

	int _params;
};

class gBuffer
{
	public:
		gBuffer();
		void add(unsigned char*);
		void del(void);
		unsigned char available(void);
		unsigned char empty(void);

		gRecord * get(void);
		gRecord * peek(void);
		
		unsigned char check(void);
		
	private:
		unsigned char _records;
		unsigned char _recordsMax;
		gRecord *_first;
		
};

class gEngine
{
	public:
		gEngine();
		void setScale(double, double, double, double);
		void cue(unsigned char*);
		void process(void);
		void processG(gRecord*);
		void processM(gRecord*);
		void processT(gRecord*);
		
		cartesian *_bed;
		unsigned char _reply[128];
		gBuffer _buff;
		
	private:
		bool			_finished;
		bool			_paused;
		unsigned long	_pauseTS;
		
		unsigned long _ts;
		
		double _xStepsMM;
		double _yStepsMM;
		double _zStepsMM;
		double _eStepsMM;
		
};
#endif /* GCODE_H_ */
